<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit Group</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>
<h3>Select the Group to Edit(Leave blank the Field not to be changed)</h3>
<form action="#" method="post">
Select Group:<br/>
<select name="name">
<option value="N/A">Select a Group from Dropdown</option>
<?php 
/*
*This code first fetches the available groups from the system and puts it into the dropdown menu.
*It then allows the user to select this option and any editings made under the specified selection will be changed 
*accordingly.
*/
$conn=mysql_connect("localhost","root","") or die("Could not Establish Connection".mysql_error());
$db=mysql_select_db("contacts_ms",$conn) or die("No DB Selected".mysql_error());
$st="EM";
$qry=mysql_query("SELECT * FROM info_group");
while($r=mysql_fetch_array($qry)){
	?>
		<option value="<?php echo $r['name'] ?>"> <?php echo $r['name'] ?></option>
		<?php
	
}
?>
</select><br/>
New Name:<br/>
<input type="text" name="newname" size="30" class="txt"><br/>
New Description:<br/>
<textarea cols="30" rows="5" placeholder="" name="description"></textarea><br/><br/>
<input type="submit" value="Edit Group" name="submit" class="sbtns">&emsp;&emsp;&emsp;
<input type="reset" name="reset" value="Cancel" class="sbtns">
</form>
<?php
if(isset($_POST['submit'])){
	$name=$_POST['name'];
$newname=$_POST['newname'];
$description=$_POST['description'];
if($newname && $description){ //This code makes changes to all fields of the group.
	$myqry=mysql_query("UPDATE info_group SET name='$newname',description='$description' WHERE name='$name' ");
}
elseif(!$newname){//This code only makes changes to the description when the Name field is empty
	$myqry=mysql_query("UPDATE info_group SET description='$description' WHERE name='$name' ");
}
elseif(!$description){//This code only makes changes to the name field of the group when the description is not provided.
	$myqry=mysql_query("UPDATE info_group SET name='$newname' WHERE name='$name' ");
}
else echo "Enter Atleast one Item to change";
if(!$myqry) $resp="Error Updating Group details, Try again later!";
else $resp="Successfully edited Group details!";
echo $resp;
}
?>
</body>
</html>